<%@page import="org.eclipse.football.League"%>
<%@page import="org.eclipse.football.PlayerList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="Style.css" />
<title>Team Page</title>
</head>
<body>

<%-- Display username and log out button--%>
<% // make sure username is initialized
String username = (String)request.getSession(true).getAttribute("username");

if(username==null){
	request.getSession().setAttribute("username", "");
	getServletContext().getRequestDispatcher("/stats.jsp").forward(request, response);
}%>

<%
// Get the league from the session*
League league = (League) session.getValue("league");
if(league == null){
	// If the league is null instantiate a new one
	league = new League("league.txt");
}

// Set the league in the session
session.putValue("league", league);
%>

<%
//create playerList for picking players
PlayerList playerList = new PlayerList();
session.putValue("playerList",playerList);
%>

<% //initialize admin string for later comparisons Aaron 11/9
String admin="admin";
session.putValue("admin", admin);
%>


<%//make list of players that have not been used
league = (League) session.getValue("league");

PlayerList unusedPlayers = (PlayerList) session.getValue("playerList");

int i = 0;
int j = 0;
int k = 0;
int index = -1;
String playername;

for (i=0;i<league.getTeams().size();i++){
	// get rid of off players
	for (j=0;j<league.getTeams().get(i).getOff_players().size();j++){
		boolean found = false;
		playername = league.getTeams().get(i).getOff_players().get(j).getName();
				
		//find player position in QBs
		for (k=0;k<unusedPlayers.getOff_players().size();k++){
			if (playername.equals(unusedPlayers.getOff_players().get(k).getName()) ){
				index = k;
				found = true;
			}
		}
		//remove player
		if( found )
		{
			unusedPlayers.getOff_players().remove(index);
		}
		
		if(!found) { //look in running backs
			for (k=0;k<unusedPlayers.getRunning_backs().size();k++){
				if (playername.equals(unusedPlayers.getRunning_backs().get(k).getName()) ){
					index = k;
					found = true;
				}
			}
			if( found ) //remove player
			{
				unusedPlayers.getRunning_backs().remove(index);
			}
		}
		
		if(!found) { //look in wide outs
			for (k=0;k<unusedPlayers.getWide_outs().size();k++){
				if (playername.equals(unusedPlayers.getWide_outs().get(k).getName()) ){
					index = k;
					found = true;
				}
			}
			if( found ) //remove player
			{
				unusedPlayers.getWide_outs().remove(index);
			}
		}
		
		if(!found) { //look in tight ends
			for (k=0;k<unusedPlayers.getTight_ends().size();k++){
				if (playername.equals(unusedPlayers.getTight_ends().get(k).getName()) ){
					index = k;
					found = true;
				}
			}
			if( found ) //remove player
			{
				unusedPlayers.getTight_ends().remove(index);
			}
		}	
	}
	
	//get rid of def players
	for (j=0;j<league.getTeams().get(i).getDef_players().size();j++){
		// player to remove
		playername = league.getTeams().get(i).getDef_players().get(j).getName();
		
		//find player position in unusedPlayers
		for (k=0;k<unusedPlayers.getDef_players().size();k++){
			if (playername.equals(unusedPlayers.getDef_players().get(k).getName()) ){
				index = k;
			}
		}

		//remove player
		if( index!=-1 )
		{
			unusedPlayers.getDef_players().remove(index);
		}
	}
	
	//get rid of kickers
	for (j=0;j<league.getTeams().get(i).getKickers().size();j++){
		playername = league.getTeams().get(i).getKickers().get(j).getName();
		
		//find player position in unusedPlayers
		for (k=0;k<unusedPlayers.getKickers().size();k++){
			if (playername.equals(unusedPlayers.getKickers().get(k).getName()) ){
				index = k;
			}
		}

		//remove player
		if( index!=-1 )
		{
			unusedPlayers.getKickers().remove(index);
		}
	
	}
}

session.putValue("unusedPlayers",unusedPlayers);
%>

<c:if test="${team == null }">
404? No team associated with this view
</c:if>

<c:if test="${team != null }">
<FORM action="/FootballStats/log_in" method="post">
<H1><c:out value="${team.getName()}" />
(Owned By <c:out value="${team.getUsername()}"/>)
</H1>
<P> Not <c:out value="${username}" />? 
<INPUT type="submit" name="button" value="Change Account">
<INPUT type="button" value="To League Page" onClick="location.href='LeagueSummary.jsp'">
</FORM>

<!-- Team Table Start -->
<!-- Team Table Header Start -->
<table border="1" cellspacing="0">
<tr>
<th align="center" valign="middle" width="100">
Offensive Player
</th>
<th align="center" valign="middle">
<form action="/FootballStats/sort_off" method="post">
<input type="hidden" name="type" value="9"></input>
<input type="submit" name="Points" value="Points"></input>
</form>
</th>
<th align="center" valign="middle">
<form action="/FootballStats/sort_off" method="post">
<input type="hidden" name="type" value="0"></input>
<input type="submit" name="Pass YDs" value="Pass YDs"></input>
</form>
</th>
<th align="center" valign="middle">
<form action="/FootballStats/sort_off" method="post">
<input type="hidden" name="type" value="1"></input>
<input type="submit" name="Pass TDs" value="Pass TDs"></input>
</form>
</th>
<th align="center" valign="middle">
<form action="/FootballStats/sort_off" method="post">
<input type="hidden" name="type" value="2"></input>
<input type="submit" name="INTs" value="INTs"></input>
</form>
</th>
<th align="center" valign="middle">
<form action="/FootballStats/sort_off" method="post">
<input type="hidden" name="type" value="3"></input>
<input type="submit" name="Rush YDs" value="Rush YDs"></input>
</form>
</th>
<th align="center" valign="middle">
<form action="/FootballStats/sort_off" method="post">
<input type="hidden" name="type" value="4"></input>
<input type="submit" name="Rush TDs" value="Rush TDs"></input>
</form>
</th>
<th align="center" valign="middle">
<form action="/FootballStats/sort_off" method="post">
<input type="hidden" name="type" value="5"></input>
<input type="submit" name="Rec. YDs" value="Rec. YDs"></input>
</form>
</th>
<th align="center" valign="middle">
<form action="/FootballStats/sort_off" method="post">
<input type="hidden" name="type" value="6"></input>
<input type="submit" name="Rec. TDs" value="Rec. TDs"></input>
</form>
</th>
<th align="center" valign="middle">
<form action="/FootballStats/sort_off" method="post">
<input type="hidden" name="type" value="7"></input>
<input type="submit" name="Fumbles" value="Fumbles"></input>
</form>
</th>
<th align="center" valign="middle">
<form action="/FootballStats/sort_off" method="post">
<input type="hidden" name="type" value="8"></input>
<input type="submit" name="Return TDs" value="Return TDs"></input>
</form>
</th>
<th>
Team
</th>
</tr>
<!-- Team Table Header End -->

<!-- Team Table Row Start -->
<c:forEach items="${team.off_players }" var="player">
<tr>
<td>
<form action="/FootballStats/pick_off" method="post">
<input type="hidden" name="name" value="${player.getName() }"></input>
<input type="submit" name="PickPlayer" value="${player.getName() }"></input>
</form>

<form action="/FootballStats/off" method="post">
<c:choose>
<c:when test="${username==team.getUsername()||username==admin}">
<input type="hidden" name="name" value="${player.getName()}"></input>
<input type="hidden" name="team" value="${player.getTeam() }"></input>
<input type="submit" name="Off Player Button" value="Fire!"></input>
</c:when>
</c:choose>
</form>

</td>
<td>
<c:out value="${player.calculatePoints() }"/>
</td>
<td>
<c:out value="${player.getPass_yds() }"/>
</td>
<td>
<c:out value="${player.getPass_tds() }"/>
</td>
<td>
<c:out value="${player.getPicks() }"/>
</td>
<td>
<c:out value="${player.getRush_yds() }"/>
</td>
<td>
<c:out value="${player.getRush_tds() }"/>
</td>
<td>
<c:out value="${player.getRec_yds() }"/>
</td>
<td>
<c:out value="${player.getRec_tds() }"/>
</td>
<td>
<c:out value="${player.getFumbles() }"/>
</td>
<td>
<c:out value="${player.getRet_tds() }"/>
</td>
<td>
<c:out value="${player.getTeam() }"/>
</td>
</tr>
</c:forEach>
</table>
<!-- Team Table Row End -->
<!-- Team Table End -->

<c:choose>
<c:when test="${username==team.getUsername()||username==admin}">
<form action="/FootballStats/off" method="post">
<br>

QB 
<SELECT name="qb">
<c:forEach items="${unusedPlayers.off_players }" var="QB">
				<OPTION value="${QB.getName() },${QB.getTeam() }">"${QB.getName() }, ${QB.getTeam() }"</OPTION>
</c:forEach>
</SELECT>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RB
<SELECT name="rb">
<c:forEach items="${unusedPlayers.getRunning_backs() }" var="RB">
				<OPTION value="${RB.getName() },${RB.getTeam() }">"${RB.getName() }, ${RB.getTeam() }"</OPTION>
</c:forEach>
</SELECT>
<br>WR
<SELECT name="wr">
<c:forEach items="${unusedPlayers.getWide_outs() }" var="WR">
				<OPTION value="${WR.getName() },${WR.getTeam() }">"${WR.getName() }, ${WR.getTeam() }"</OPTION>
</c:forEach>
</SELECT>
&nbsp;&nbsp;&nbsp;TE
<SELECT name="te">
<c:forEach items="${unusedPlayers.getTight_ends() }" var="TE">
				<OPTION value="${TE.getName() },${TE.getTeam() }">"${TE.getName() }, ${TE.getTeam() }"</OPTION>
</c:forEach>
</SELECT>
<BR>
<BR>
<input type="submit" name="Off Player Button" value="Add Offensive Player">
</form>
</c:when>
</c:choose>
<br>

<!-- Player Table Start -->
<!-- Player Table Header Start -->
<table border="1" cellspacing="0">
<tr>
<th align="center" valign="middle" width="100">Defense/Special Teams</th>
<th align="center" valign="middle">
<form action="/FootballStats/sort_def" method="post">
<input type="hidden" name="type" value="8"></input>
<input type="submit" name="Points" value="Points"></input>
</form>
</th>
<%--<th align="center" valign="middle">
<form action="/FootballStats/sort_def" method="post">
<input type="hidden" name="type" value="0"></input>
<input type="submit" name="Defensive Touchdowns" value="Defensive Touchdowns"></input>
</form>
</th>--%>
<%--<th align="center" valign="middle">
<form action="/FootballStats/sort_def" method="post">
<input type="hidden" name="type" value="1"></input>
<input type="submit" name="Defensive Interceptions" value="Defensive Interceptions"></input>
</form>
</th>--%>
<%--<th align="center" valign="middle">
<form action="/FootballStats/sort_def" method="post">
<input type="hidden" name="type" value="2"></input>
<input type="submit" name="Fumbles Recovered" value="Fumbles Recovered"></input>
</form>
</th>--%>
<%--<th align="center" valign="middle">
<form action="/FootballStats/sort_def" method="post">
<input type="hidden" name="type" value="3"></input>
<input type="submit" name="Sacks" value="Sacks"></input>
</form>
</th>--%>
<%--<th align="center" valign="middle">
<form action="/FootballStats/sort_def" method="post">
<input type="hidden" name="type" value="4"></input>
<input type="submit" name="Blocked Kicks" value="Blocked Kicks"></input>
</form>
</th>--%>
<%--<th align="center" valign="middle">
<form action="/FootballStats/sort_def" method="post">
<input type="hidden" name="type" value="5"></input>
<input type="submit" name="Safeties" value="Safeties"></input>
</form>
</th>--%>
<th align="center" valign="middle">
<form action="/FootballStats/sort_def" method="post">
<input type="hidden" name="type" value="6"></input>
<input type="submit" name="Points Allowed" value="Points Allowed"></input>
</form>
</th>
<%--<th align="center" valign="middle">
<form action="/FootballStats/sort_def" method="post">
<input type="hidden" name="type" value="7"></input>
<input type="submit" name="Yards Allowed" value="Yards Allowed"></input>
</form>
</th>--%>
</tr>
<!-- Player Table Header End -->

<!-- Player Table Row Start -->
<c:forEach items="${team.def_players }" var="player">
<tr>
<td>
<form action="/FootballStats/pick_def" method="post">
<input type="hidden" name="name" value="${player.getName() }"></input>
<input type="submit" name="PickPlayer" value="${player.getName() }"></input>
</form>

<form action="/FootballStats/def" method="post">
<c:choose>
<c:when test="${username==team.getUsername()||username==admin}">
<input type="hidden" name="name" value="${player.getName()}"></input>
<input type="submit" name="Def Player Button" value="Fire!"></input>
</c:when>
</c:choose>
</form>

</td>
<td>
<c:out value="${player.calculatePoints() }"/>
</td>
<%--<td>
<c:out value="${player.getDef_tds() }"/>
</td>
<td>
<c:out value="${player.getPicks() }"/>
</td>
<td>
<c:out value="${player.getFumbles() }"/>
</td>
<td>
<c:out value="${player.getSacks() }"/>
</td>
<td>
<c:out value="${player.getBlocks() }"/>
</td>
<td>
<c:out value="${player.getSafeties() }"/>
</td>--%>
<td>
<c:out value="${player.getPa() }"/>
</td>
<%--<td>
<c:out value="${player.getYa() }"/>
</td>--%>
</tr>
</c:forEach>
</table>
<!-- Player Table Row End -->
<!-- Player Table End -->

<c:choose>
<c:when test="${username==team.getUsername()||username==admin}">
<form action="/FootballStats/def" method="post">
<br>
<input type="submit" name="Def Player Button" value="Add Defensive Player">
<SELECT name="name">
<c:forEach items="${unusedPlayers.def_players }" var="player">
				<OPTION value="${player.getName() }">"${player.getName() }"</OPTION>
</c:forEach>
</SELECT>
</form>
</c:when>
</c:choose>
<br>

<table border="1" cellspacing="0">
<tr>
<th align="center" valign="middle" width="100">Kicker</th>
<th align="center" valign="middle">
<form action="/FootballStats/sort_kicker" method="post">
<input type="hidden" name="type" value="3"></input>
<input type="submit" name="Points" value="Points"></input>
</form>
</th>
<th align="center" valign="middle">
<form action="/FootballStats/sort_kicker" method="post">
<input type="hidden" name="type" value="0"></input>
<input type="submit" name="PAT Made" value="PAT Made"></input>
</form>
</th>
<th align="center" valign="middle">
<form action="/FootballStats/sort_kicker" method="post">
<input type="hidden" name="type" value="1"></input>
<input type="submit" name="FG Made" value="FG Made"></input>
</form>
</th>
<th align="center" valign="middle">
<form action="/FootballStats/sort_kicker" method="post">
<input type="hidden" name="type" value="2"></input>
<input type="submit" name="FG Missed" value="FG Missed"></input>
</form>
</th>
<th>
Team
</th>
</tr>

<c:forEach items="${team.kickers }" var="player">
<tr>
<td>
<form action="/FootballStats/pick_kicker" method="post">
<input type="hidden" name="name" value="${player.getName() }"></input>
<input type="submit" name="PickPlayer" value="${player.getName() }"></input>
</form>

<form action="/FootballStats/kicker" method="post">
<c:choose>
<c:when test="${username==team.getUsername()||username==admin}">
<input type="hidden" name="name" value="${player.getName()}"></input>
<input type="hidden" name="team" value="${player.getTeam() }"></input>
<input type="submit" name="Kicker Button" value="Fire!"></input>
</c:when>
</c:choose>
</form>

</td>
<td>
<c:out value="${player.calculatePoints() }"/>
</td>
<td>
<c:out value="${player.getPat() }"/>
</td>
<td>
<c:out value="${player.getFg() }"/>
</td>
<td>
<c:out value="${player.getMisses() }"/>
</td>
<td>
<c:out value="${player.getTeam() }"/>
</td>
</tr>
</c:forEach>
</table>

<c:choose>
<c:when test="${username==team.getUsername()||username==admin}">
<form action="/FootballStats/kicker" method="post">
<br>
Kicker
<SELECT name="kicker">
<c:forEach items="${unusedPlayers.kickers }" var="KC">
				<OPTION value="${KC.getName() },${KC.getTeam() }">"${KC.getName() }, ${KC.getTeam() }"</OPTION>
</c:forEach>
</SELECT>
<input type="submit" name="Kicker Button" value="Add Kicker">
</form>
</c:when>
</c:choose>
<br>

<BR>
<c:choose>
<c:when test="${username==team.getUsername()||username==admin}">
<form action="/FootballStats/teams" method="post">
<input type="submit" name="Team Button" value="Delete Team">
</form>

</c:when>
</c:choose>



<c:if test="${off != null }">
<form action="/FootballStats/off_stats" method="post">
Passing Yards: <br><input type="text" name="pass_yds" size="35">
<br>
Passing Touchdowns: <br><input type="text" name="pass_tds" size="35">
<br>
Interceptions: <br><input type="text" name="picks" size="35">
<br>
Rushing Yards: <br><input type="text" name="rush_yds" size="35">
<br>
Rushing Touchdowns: <br><input type = "text" name="rush_tds" size="35">
<br>
Receiving Yards: <br><input type="text" name="rec_yds" size="35">
<br>
Receiving Touchdowns: <br><input type="text" name="rec_tds" size="35">
<br>
Fumbles: <br><input type="text" name="fumbles" size="35">
<br>
Return Touchdowns: <br><input type="text" name="ret_tds" size="35">
<br>
<br>
<input type="submit" name="Add Stats" value="Add Stats">
</form>
</c:if>

<c:if test="${def != null }">
<form action="/FootballStats/def_stats" method="post">
Defensive Touchdowns: <br><input type="text" name="def_tds" size="35">
<br>
Defensive Interceptions: <br><input type="text" name="picks" size="35">
<br>
Fumbles Recovered: <br><input type="text" name="fumbles" size="35">
<br>
Sacks: <br><input type="text" name="sacks" size="35">
<br>
Blocked Kicks: <br><input type="text" name="blocks" size="35">
<br>
Safeties: <br><input type = "text" name="safeties" size="35">
<br>
Points Allowed: <br><input type="text" name="pa" size="35">
<br>
Yards Allowed: <br><input type="text" name="ya" size="35">
<br>
<br>
<input type="submit" name="Add Stats" value="Add Stats">
</form>
</c:if>

<c:if test="${kicker != null }">
<form action="/FootballStats/kicker_stats" method="post">
PAT Made: <br><input type="text" name="pat" size="35">
<br>
FG Made: <br><input type="text" name="fg" size="35">
<br>
Misses: <br><input type="text" name="misses" size="35">
<br>
<br>
<input type="submit" name="Add Stats" value="Add Stats">
</form>
</c:if>

</c:if>
</body>
</html>